package ${ob.pkg};

<#list ob.imports as imps>
import ${imps};
</#list>
import java.util.List;
import java.util.ArrayList;
import ${ob.e.pkg}.${ob.e.className};
import ${ob.parent};
import ${ob.comparator};


/**
 *	OrderBy (${ob.e.tableName})
 *  ${ob.e.tableDesc!""}
 *
 *	@author ${ob.e.author!""} (${ob.e.authorEnglish!""})
 *	@date ${ob.e.createTime}
 */
public class ${ob.className} extends ${ob.parentName}<${ob.e.className}> {

    /**
     *  主要用于分表查询时的汇总排序
     *  <p> 当分表查询跨多个分表，同时查询又带有排序，需要额外的逻辑将多个分表的查询结果汇总
     */
    public ${ob.className} comparator(${ob.comparatorClassName}<${ob.e.className}> comparator) {
        this.comparator = comparator;
        return this;
    }

    public ${ob.className} setCurrentItemIdx(int itemIdx) {
        this.itemIdx = itemIdx;
        return this;
    }
    public ${ob.className} setCurrentItemVal(${ob.e.className} e) {
        if (e == null) {return this;}
        Item item = getCurrentItem();
        item.setVal(getVal(e, item));
        return this;
    }

    <#list ob.fields as field>
    public static String ${field.property} = "${field.column}";
    </#list>

    public static ${ob.className} build(String field, boolean isAsc) {
        ${ob.className} ob = new ${ob.className}();
        ob.items = new ArrayList<>();
        ob.items.add(Item.build(field, isAsc));
        return ob;
    }
    public static ${ob.className} ASC(String field) { return build(field, true); }
    public static ${ob.className} DESC(String field) { return build(field, false); }

    public ${ob.className} Aas(String field) { items.add(Item.build(field, true)); return this; }
    public ${ob.className} Desc(String field) { items.add(Item.build(field, false)); return this; }

    /**
     *  根据字段名取值
     */
    public Object getVal(${ob.e.className} e, Item item) {
        <#list ob.fields as f>
        if (item.getColumn().equals(${f.property})) {return e.get${f.property}();}
        </#list>
        return null;
    }
}